package com.jdrx.ocp.service;

import com.jdrx.ocp.beans.constans.EDeleteFlag;
import com.jdrx.ocp.beans.dto.dept.QueryOpenDeptDTO;
import com.jdrx.ocp.beans.dto.role.QueryOpenRoleDTO;
import com.jdrx.ocp.beans.dto.user.QueryOpenUserDTO;
import com.jdrx.ocp.beans.dto.user.QueryUserResourceDTO;
import com.jdrx.ocp.beans.entity.DeptPO;
import com.jdrx.ocp.beans.entity.RolePO;
import com.jdrx.ocp.beans.entity.util.ExceptionMessageFilter;
import com.jdrx.ocp.beans.vo.*;
import com.jdrx.ocp.dao.DeptDAO;
import com.jdrx.ocp.dao.RoleDAO;
import com.jdrx.ocp.dao.UserDAO;
import com.jdrx.platform.commons.rest.exception.BizException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * 对外开放数据查询接口
 * @Author: Huangxg
 * @Date: 2018/11/22 14:44
 */
@Service
public class OpenService {

    private final static Logger logger = LoggerFactory.getLogger(OpenService.class);

    @Autowired
    DeptDAO deptDAO;
    @Autowired
    RoleDAO roleDAO;
    @Autowired
    UserDAO userDAO;

    @Autowired
    UserService userService;

    /**
     * 部门查询
     * @param queryOpenDeptDTO
     * @return
     * @throws BizException
     */
    public List<QueryDeptVO> queryDept(QueryOpenDeptDTO queryOpenDeptDTO) throws BizException {
        try{
            Map<String,Object> queryMap = new HashMap<>(4);
            queryMap.put("name",queryOpenDeptDTO.getName());
            List<DeptPO> list = deptDAO.listBy(queryMap);
            List<QueryDeptVO> voList = new ArrayList<>();
            list.stream().forEach(item->{
                QueryDeptVO queryDeptVO = new QueryDeptVO();
                BeanUtils.copyProperties(item,queryDeptVO);
                voList.add(queryDeptVO);
            });
            return voList;
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            throw new BizException("获取数据失败！");
        }
    }

    /**
     * 角色查询
     * @param queryOpenRoleDTO
     * @return
     * @throws BizException
     */
    public List<QueryRoleVO> queryRole(QueryOpenRoleDTO queryOpenRoleDTO) throws BizException {
        try{
            Map<String,Object> queryMap = new HashMap<>(4);
            queryMap.put("name",queryOpenRoleDTO.getName());
            List<RolePO> list = roleDAO.listBy(queryMap);
            List<QueryRoleVO> voList = new ArrayList<>();
            list.stream().forEach(item->{
                QueryRoleVO queryRoleVO = new QueryRoleVO();
                BeanUtils.copyProperties(item,queryRoleVO);
                voList.add(queryRoleVO);
            });
            return voList;
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            throw new BizException("获取数据失败！");
        }
    }

    /**
     * 用户查询
     * @param queryOpenUserDTO
     * @param prefix
     * @return
     * @throws BizException
     */
    public List<QueryOpenUserVO> queryUser(QueryOpenUserDTO queryOpenUserDTO,String prefix) throws BizException {
        try{
            Map<String,Object> queryMap = new HashMap<>(4);
            queryMap.put("name",queryOpenUserDTO.getName());
            queryMap.put("deptId",queryOpenUserDTO.getDeptId());
            //只能获取非运维人员的帐户信息
            queryMap.put("isSystem",0);
            if("gis".equals(prefix)){
                queryMap.put("isAdmin",0);
            }
            return userDAO.listByOpen(queryMap);
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            throw new BizException("获取数据失败！");
        }
    }

    /**
     * 获取用户菜单
     * @param queryUserResourceDTO
     * @return
     * @throws BizException
     */
    public List<QueryUserResourceVO> getUserMenu(QueryUserResourceDTO queryUserResourceDTO , String tid) throws BizException {
        try{
//            queryUserResourceDTO.setType(EResourceType.MENU.getKey());
            return userService.queryUserMenu(queryUserResourceDTO , tid);
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            throw new BizException("获取数据失败！");
        }
    }
    /**
     * 获取用户权限资源
     * @param queryUserResourceDTO
     * @return
     * @throws BizException
     */
    public List<QueryUserResourceVO> getUserResource(QueryUserResourceDTO queryUserResourceDTO , String tid) throws BizException {
        try{
//            queryUserResourceDTO.setType(EResourceType.BUTTON.getKey());
            return userService.queryUserResource(queryUserResourceDTO , tid);
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            throw new BizException("获取数据失败！");
        }
    }

    /**
     * 根据ID查询用户
     * @param userId
     * @return
     * @throws BizException
     */
    public QueryOpenUserVO queryUserById(Long userId) throws BizException {
        try{
            Map<String,Object> queryMap = new HashMap<>(4);
            queryMap.put("id",userId);
            List<QueryOpenUserVO> users = userDAO.listByOpen(queryMap);
            if(CollectionUtils.isEmpty(users)){
                throw new BizException("该用户不存在");
            }
            return users.get(0);
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            throw new BizException(ExceptionMessageFilter.filter(e.getMessage()));
        }
    }

    /**
     * 获取所有用户
     * @return
     * @throws BizException
     */
    public List<QueryOpenUserVO> getUserAll(QueryOpenUserDTO queryOpenUserDTO) throws BizException {
        try{
            Map<String,Object> queryMap = new HashMap<>(1);
            queryMap.put("name",queryOpenUserDTO.getName());
            queryMap.put("deptId",queryOpenUserDTO.getDeptId());
            queryMap.put("delFlag",EDeleteFlag.NORMAL.getKey());
            return userDAO.listByOpen(queryMap);
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            throw new BizException("获取数据失败！");
        }
    }

}
